package com.asset.modules.common.servcie;


import com.asset.modules.common.utils.Constant;
import com.asset.modules.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 价值计算服务
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CalcValueService {


    /**
     * 计算净值
     * @param primaryValue 基础价格
     * @param limitedYear  年限
     * @param buyTime      购入时间
     * @return
     */
    public static  Double calcAssetValue(Double primaryValue, int limitedYear, String buyTime){
        String currentYear = DateUtils.formatDateToString(new Date(), DateUtils.DATE_FORMAT_Y);
        int passedYear = Integer.valueOf(currentYear) - Integer.valueOf(buyTime.substring(0,4));
        Double currentValue= 0.00;
        DecimalFormat df = new DecimalFormat("0.00");
        Double yearRate = Double.valueOf(df.format((float)passedYear/limitedYear));
        Double tempValue = primaryValue - (1-0.03)*primaryValue*yearRate;
        currentValue = tempValue > primaryValue*0.03? tempValue:primaryValue*0.03;
        return currentValue;
    }



}
